package com.aqie.easy.linkedlist;

import com.aqie.easy.structure.ListNode;

import java.util.*;

/**
 * 876. 链表的中间结点
 * 集合输出一共有四种手段：Iterator、ListIterator、Enumeration、foreach
 */
public class MiddleNode {
    /**
     * 快慢指针
     * @param head
     * @return
     */
    public ListNode middleNode(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode slow = head, fast = head;
        while (fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

    public static void main(String[] args) {
        Integer [] arr = {1,2,3,4,5};
        // array 转 list
        List<Integer> list = new ArrayList<>(Arrays.asList(arr));
        for (Integer integer : list) {
            System.out.println(integer);
        }
        System.out.println(list);
    }
}
